package edu.集合基础.ArrayList;

import java.util.ArrayList;

public class ArrayList方法 {
    public static void main(String[] args) {
        ArrayList<String> list=new ArrayList<>();
        list.add("123");
        list.add("456");
        list.add("789");
        list.add("101");

        /*查询*/

        //public E get(int index):返回指定索引处的元素
        String s = list.get(0);
        String s1 = list.get(1);
        String s2 = list.get(2);
        System.out.println(s+",,"+s1+",,"+s2);//123,,456,,789

        //public int size():返回集合中元素的个数
        int size = list.size();
        System.out.println(size);//4

    }

    public static void testSet() {
        ArrayList<String> list=new ArrayList<>();
        list.add("123");
        list.add("456");
        list.add("789");
        list.add("101");

        /*修改*/

        //public E set(int index,E element):修改指定索引处的元素，返回被修改的元素
        String set = list.set(0, "666");
        System.out.println(set);//123
        System.out.println(list);//[666, 456, 789, 101]
    }

    public static void testRemove() {//ctrl+alt+M创建方法
        ArrayList<String> list=new ArrayList<>();
        list.add("123");
        list.add("456");
        list.add("789");
        list.add("101");

        /*删除*/
        //public boolean remove(Object o):删除指定的元素，返回删除是否成功
        boolean remove = list.remove("101");//true
        boolean remove1 = list.remove("111");//false,删除不存在的元素，返回的是false
        System.out.println(list);//[123, 456, 789]

        //public E remove(int index)：根据索引删,返回的是删除掉的元素
        //String remove2 = list.remove(0);一般不会接受返回值
        list.remove(0);
        System.out.println(list);
    }
}
